<!--
 * @Author: your name
 * @Date: 2021-11-04 19:27:06
 * @LastEditTime: 2021-11-04 19:53:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ej-app2\src\views\Login.vue
-->
<template>
    <h1>登录页面</h1>
    <van-form>
        <van-cell-group inset>
            <van-field
            v-model="data.username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
            v-model="data.password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
            />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit" @click="onsubmit">
            提交
            </van-button>
        </div>
    </van-form>
</template>

<script>
import { ref,reactive } from 'vue';
import { post_json } from '../utils/axios';
import {setToken} from "@/utils/auth";
export default {
    // 初始默认显示
    setup(){
        var data=reactive({username:'custome1',password:'123321'});
        return { data };
    },
    methods:{
        onsubmit(){
            post_json('/user/login',this.data).then((res)=>{
                // console.log(res.data)
                // 首先判断token是否存在
                if(res.data.token){
                    setToken(res.data.token)
                    this.$router.push('/');
                }
            })
        }
    }
}
</script>